<?php
/**
 * Created by PhpStorm.
 * User: Adminstratot
 * Date: 2018/11/12
 * Time: 13:14
 */
namespace Admin\Controller;
use Think\Controller;
class SearchController extends Controller{
    public function Search(){
        $this->verification();
        if(IS_POST){
            //获取文件栏目
            $this->filecate(1);
            //获取文章栏目
            $this->artcate(2);
            //获取相册栏目
            $this->albumlist();
            //获取用户名单
            $this->userlist();

            //获取传过来的文件栏目ID和相册ID与要搜索的关键词
            $cid=I('post.cid');
            $aid=I('post.aid');
            $tid=I('post.tid');
            $keywords=I('post.search');

            //模糊查询条件s
            $file_name['ori_name'] =array('like',array("%$keywords%"));
            $file_intro['intro'] =array('like',array("%$keywords%"));
            $picture['intro'] =array('like',array("%$keywords%"));
            $article_title['title'] =array('like',array("%$keywords%"));
            $article_label['label'] =array('like',array("%$keywords%"));

            //添加条件为非回收站
            $file_name['is_del'] =array('eq',0);
            $file_intro['is_del'] =array('eq',0);
            $picture['is_del'] =array('eq',0);
            $article_title['is_del'] =array('eq',0);
            $article_label['is_del'] =array('eq',0);

            //判断是否有选择栏目查询
            if($cid==0 && $aid==0 && $tid==0){         //没有选择栏目的情况
                $searchres[]=D('file')->where($file_name)->select();
                $searchres[]=D('file')->where($file_intro)->select();
                $searchres[]=D('article')->where($article_title)->select();
                $searchres[]=D('article')->where($article_label)->select();
                $searchres[]=D('picture')->where($picture)->select();
                $this->query($searchres);
            }else if($cid!==0 && $aid==0 && $tid==0){          //只选择文件栏目
                $file_name['cid'] =array('eq',$cid);
                $file_intro['cid'] =array('eq',$cid);
                $searchres[]=D('file')->where($file_name)->select();
                $searchres[]=D('file')->where($file_intro)->select();
                $this->query($searchres);
            }else if($cid==0 && $aid!==0 && $tid==0) {          //只选择相册
                $picture['aid'] = array('eq', $aid);
                $searchres[] = D('picture')->where($picture)->select();
                $this->query($searchres);
            }else if($cid==0 && $aid==0 && $tid!==0) {           //只选择文章
                $article_title['cid'] = array('eq', $tid);
                $article_label['cid'] = array('eq', $tid);
                $searchres[] = D('article')->where($article_title)->select();
                $searchres[] = D('article')->where($article_label)->select();
                $this->query($searchres);
            }else if($cid!==0 && $aid!==0 && $tid==0) {          //选择了文件与相册栏目
                $file_name['cid'] = array('eq', $cid);
                $file_intro['cid'] = array('eq', $cid);
                $picture['aid'] = array('eq', $aid);
                $searchres[] = D('file')->where($file_name)->select();
                $searchres[] = D('file')->where($file_intro)->select();
                $searchres[] = D('picture')->where($picture)->select();
                $this->query($searchres);
            }else if($cid!==0 && $aid==0 && $tid!==0){          //选择了文件与文章栏目
                $file_name['cid'] = array('eq', $cid);
                $file_intro['cid'] = array('eq', $cid);
                $article_title['cid'] = array('eq', $tid);
                $article_label['cid'] = array('eq', $tid);
                $searchres[] = D('file')->where($file_name)->select();
                $searchres[] = D('file')->where($file_intro)->select();
                $searchres[] = D('article')->where($article_title)->select();
                $searchres[] = D('article')->where($article_label)->select();
                $this->query($searchres);
            }else if($cid==0 && $aid!==0 && $tid!==0){          //选择了相册与文章栏目
                $article_title['cid'] = array('eq', $tid);
                $article_label['cid'] = array('eq', $tid);
                $picture['aid'] = array('eq', $aid);
                $searchres[] = D('article')->where($article_title)->select();
                $searchres[] = D('article')->where($article_label)->select();
                $searchres[] = D('picture')->where($picture)->select();
                $this->query($searchres);
            }else if($cid!==0 && $aid!==0 && $tid!==0){         //都选择的情况
                $file_name['cid'] =array('eq',$cid);
                $file_intro['cid'] =array('eq',$cid);
                $article_title['cid'] = array('eq', $tid);
                $article_label['cid'] = array('eq', $tid);
                $picture['aid'] =array('eq',$aid);
                $searchres[]=D('file')->where($file_name)->select();
                $searchres[]=D('file')->where($file_intro)->select();
                $searchres[]=D('article')->where($article_title)->select();
                $searchres[]=D('article')->where($article_label)->select();
                $searchres[]=D('picture')->where($picture)->select();
                $this->query($searchres);
            }
        }
    }

    //验证是否登陆
    protected function verification(){
        $userModel=D('user');
        $userinfo=$userModel->where(array('account'=>cookie('account')))->find();
        if(!che() || !$userinfo){
            $url=U('User/login');
            echo "<script>alert('对不起，请您先登陆。');location.href='$url';</script>";
        }
    }

    protected function query($searchres){
        if($this->arrayToString($searchres)){
            $search=array();
            for($i=0;$i<count($searchres);$i++){
                $search=array_merge($search,$searchres[$i]);
            }
            $this->assign('searchres',$search);
            $this->display();
        }else{
            echo "<script>alert('对不起，查询不到相关数据!');location.href='javascript:history.go(-1)';</script>";
        }
    }

    //将数组转换成字符串格式
    protected function arrayToString($arr){
        if(is_array($arr)){
            return implode('', array_map([$this,'arrayToString'],$arr));
        }
        return $arr;
    }

    //获取文件栏目
    protected function filecate($pid){
        $cateModel=D('cate');
        $filecate=$cateModel->gettree($pid);
        $this->assign('filecate',$filecate);
    }

    //获取文章栏目
    protected function artcate($pid){
        $cateModel=D('cate');
        $artcate=$cateModel->gettree($pid);
        $this->assign('artcate',$artcate);
    }

    //获取搜索栏的相册栏目
    protected function albumlist(){
        $albumModel=D('album');
        $albumlist=$albumModel->select();
        $this->assign('albumlist',$albumlist);
    }

    //获取用户列表
    protected function userlist(){
        $userModel=D('userinfo');
        $userlist=$userModel->select();
        $this->assign('userlist',$userlist);
    }

    /*
    public function page(){
        $queryModel=D('file');
        $start=isset($_GET['start'])?$_GET['start']+0:0;
        $pagecount = 10;
        $query_file=$queryModel->where('is_del=0 and cid=' . I('cid'))->order('fid desc')->limit($start . ',' . $pagecount)->select();
        $query_all=$queryModel->where('is_del=0 and cid=' . I('cid'))->select();
        if($query_file === false || $query_all === false) {
            echo "执行失败";
        }else {
            //$query_rowcount = count($query_file);
            $query_count = count($query_all);
        }
        //$this->assign('file_rowcount',$query_rowcount);
        $this->assign('file_count',$query_count);
        $this->assign('pagecount',$pagecount);
        $this->assign('file_list',$query_file);
        //$this->assign('file_all',$query_all);
    }
    */
}
?>